﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Basics
{
    class Tank : DrawableObject, ITankController//, ICollidable
    {
        private float speed;
        private TankBody tankBody;
        private TankTurret tankTurret;

        private FacingDirection newFacingDirection = FacingDirection.NORTH;

        public float Speed
        {
            get { return this.speed; }
            set { this.speed = value; }
        }
        

        public Tank(Texture2D bodyTexture, Texture2D turretTexture, float speed, Vector2 position)
        {
            this.tankBody = new TankBody(bodyTexture, 0.1f);
            this.tankTurret = new TankTurret(turretTexture, 0.09f);
            this.speed = speed;
            this.Position = position;
            this.facingDirection = newFacingDirection;
            this.Update(position, FacingDirection.NORTH);
        }

        public void MoveUp()
        {
            this.Position = this.Position + new Vector2(0, -1) * this.speed * (float)Program.game.gameTime.ElapsedGameTime.TotalSeconds;
            if (this.newFacingDirection == 0)
                this.newFacingDirection = FacingDirection.NORTH;
            else
                this.newFacingDirection = this.newFacingDirection | FacingDirection.NORTH;
        }

        public void MoveDown()
        {
            this.Position = this.Position + new Vector2(0, 1) * this.speed * (float)Program.game.gameTime.ElapsedGameTime.TotalSeconds;
            if (this.newFacingDirection == 0)
                this.newFacingDirection = FacingDirection.SOUTH;
            else
                this.newFacingDirection = this.newFacingDirection | FacingDirection.SOUTH;
        }

        public void MoveLeft()
        {
            this.Position = this.Position + new Vector2(-1, 0) * this.speed * (float)Program.game.gameTime.ElapsedGameTime.TotalSeconds;
            if (this.newFacingDirection == 0)
                this.newFacingDirection = FacingDirection.WEST;
            else
                this.newFacingDirection = this.newFacingDirection | FacingDirection.WEST;
        }

        public void MoveRight()
        {
            this.Position = this.Position + new Vector2(1, 0) * this.speed * (float)Program.game.gameTime.ElapsedGameTime.TotalSeconds;
            if (this.newFacingDirection == 0)
                this.newFacingDirection = FacingDirection.EAST;
            else
                this.newFacingDirection = this.newFacingDirection | FacingDirection.EAST;
        }

        public void RotateTurret(double angle)
        {
            this.tankTurret.Rotate(angle);
        }

        override public void Draw(SpriteBatch spriteBatch)
        {
            this.Update(this.Position, newFacingDirection);
            this.tankBody.Draw(spriteBatch);
            this.tankTurret.Draw(spriteBatch);
        }

        private void Update(Vector2 position, FacingDirection facingDirection)
        {
            this.tankTurret.Position = position;
            this.tankBody.Position = position;

            facingDirection = newFacingDirection;

            switch (facingDirection)
            {
                case FacingDirection.NORTH:
                    this.tankBody.Rotate(0);
                    break;
                case FacingDirection.EAST:
                    this.tankBody.Rotate(MathHelper.PiOver2);
                    break;
                case FacingDirection.SOUTH:
                    this.tankBody.Rotate(MathHelper.Pi);
                    break;
                case FacingDirection.WEST:
                    this.tankBody.Rotate(MathHelper.Pi + MathHelper.PiOver2);
                    break;
                case FacingDirection.NORTH_EAST:
                    this.tankBody.Rotate(MathHelper.PiOver4);
                    break;
                case FacingDirection.SOUTH_EAST:
                    this.tankBody.Rotate(MathHelper.PiOver2 + MathHelper.PiOver4);
                    break;
                case FacingDirection.SOUTH_WEST:
                    this.tankBody.Rotate(MathHelper.Pi + MathHelper.PiOver4);
                    break;
                case FacingDirection.NORTH_WEST:
                    this.tankBody.Rotate(MathHelper.Pi + MathHelper.PiOver2 + MathHelper.PiOver4);
                    break;
                default:
                    break;
            }

            this.newFacingDirection = 0;
        }
    }
}
